package com.city.springcloud.controller;

import com.city.springcloud.entity.Dept;
import com.city.springcloud.service.DeptDaoService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * User: Cheng
 * Date: 2020/4/25
 * Time: 23:19
 * Description: No Description
 *
 * 提供者服务
 * 通过maven导入实体类操作，提供服务给消费者
 *
 * 熔断，处理服务提供者发生错误
 */
@RestController
public class DetpContrellor {
    @Autowired
    private DeptDaoService deptDaoService;

    @GetMapping("/dept/deptById/{id}")
    @HystrixCommand(fallbackMethod = "hystrixDeptById")//熔断
    public Dept deptById(@PathVariable("id") Long id){
        Dept dept = deptDaoService.deptById(id);
        //访问该服务时如果id为1则阻塞，需要进行熔断处理
        if (id.equals(1L)) {
            throw new RuntimeException("id:"+id+"不存在");
        }
        return dept;
    }
    //熔断，备选方案，处理一些trycart处理不了的错误，出现无论多少个异常都会熔断，不用意义解决
    public Dept hystrixDeptById(@PathVariable("id") Long id){
        //直接让用户拿到错误的信息提示，让程序继续执行走下去，但是资源耗了，服务也没提供到
        return new Dept().setDeptId(id).setDeptName("id："+id+":没有正确的信息，null--@hystirx").setDbSource("no this dataBatis in mysql");
    }
}